package com.mephi.model;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * 
 * Tree model which describes XML document structure.
 * @author mephisto
 * 1.0
 * 2011-05-30
 */
class DOMTreeModel implements TreeModel
{
	/**
	 * Creates tree model by using Document.
	 * @param doc the doc to set
	 */
	public DOMTreeModel(Document doc)
    {
		this.doc = doc;
    }

    public Object getRoot()
    {
        return doc.getDocumentElement();
    }

    public int getChildCount(Object parent)
    {
        Node node = (Node) parent;
        NodeList list = node.getChildNodes();
        return list.getLength();
    }

    public Object getChild(Object parent, int index)
    {
        Node node = (Node) parent;
        NodeList list = node.getChildNodes();
        return list.item(index);
    }

    public int getIndexOfChild(Object parent, Object child)
    {
        Node node = (Node) parent;
        NodeList list = node.getChildNodes();
        for (int i = 0, length = list.getLength(); i < length; i++)
           if (getChild(node, i) == child) return i;
        return -1;
    }

    public boolean isLeaf(Object node)
    {
        return getChildCount(node) == 0;
    }

    public void valueForPathChanged(TreePath path, Object newValue){}

    public void addTreeModelListener(TreeModelListener l){}

    public void removeTreeModelListener(TreeModelListener l){}

    
    private Document doc;
}
